package com.clouddot.baipai.ui.fragment.H_MainFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.model.beam.getRecord.attentionRecord;
import com.clouddot.baipai.model.beam.getRecord.classAttendRecord;
import com.clouddot.baipai.model.beam.getRecord.getRecordByIdResponse;
import com.clouddot.baipai.model.beam.getRecord.getRecordResponse;
import com.clouddot.baipai.ui.adapter.A_recordByIdRecyclerAdapter;
import com.clouddot.baipai.ui.adapter.A_recordRecyclerAdapter;
import com.clouddot.baipai.ui.fragment.BaseFragment;
import com.clouddot.baipai.ui.util.SelectedBtnUtil;
import com.clouddot.baipai.ui.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxy on 2018/4/10.
 */

public class H_AttendanceFragment extends BaseFragment implements View.OnClickListener{

    private View view;
    private Context context;
    private RecyclerView recyclerView;
    private A_recordByIdRecyclerAdapter classRecyclerAdapter;
    private A_recordRecyclerAdapter classRecyclerAdapter1;
    private List<attentionRecord> list  = new ArrayList<attentionRecord>();
    private List<classAttendRecord> list1  = new ArrayList<classAttendRecord>();

    private TextView tv1;
    private LinearLayout btn1;
    private TextView name;
    private TextView [] textViews = new TextView[1];
    private LinearLayout[] linearLayouts = new LinearLayout[1];
    private Animation animation;
    private LinearLayout constraintLayout1;
    private ConstraintLayout constraintLayout2;
    private LinearLayout last,next;

    private int pageCount;
    private int CurrentPage = 1;
//    private RelativeLayout scanCard;

    private TextView title;
    private TextView classNum,attendNum;
    private RelativeLayout Rvattentd;


    private Boolean isRunning = false;
    private RelativeLayout relativeLayout;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return view;
    }

    @Override
    protected void setContentView() {
        super.setContentView();

        view = LayoutInflater.from(context).inflate(R.layout.h_f_attendancefg,null);
    }

    @Override
    protected void initControls() {
        super.initControls();

        tv1 = view.findViewById(R.id.tv1);

        btn1 = view.findViewById(R.id.btn1);


        textViews[0] = tv1;

        linearLayouts[0] = btn1;


        linearLayouts[0].setSelected(true);
        textViews[0].setSelected(true);

        animation = AnimationUtils.loadAnimation(context,R.anim.buttom_click_anim);
        animation.setDuration(200);

        name = view.findViewById(R.id.name);


        recyclerView = view.findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new GridLayoutManager(context,4));
//        recyclerView.addItemDecoration(new DivierItemGrid(context));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        classRecyclerAdapter = new A_recordByIdRecyclerAdapter(context,list);
        recyclerView.setAdapter(classRecyclerAdapter);

        constraintLayout1 = view.findViewById(R.id.left);
        constraintLayout2 = view.findViewById(R.id.right);

        constraintLayout1.startAnimation(animation1);
        constraintLayout2.startAnimation(animation2);
        last = view.findViewById(R.id.last);
        next = view.findViewById(R.id.next);

        relativeLayout = view.findViewById(R.id.loading);
        ImageView loading = view.findViewById(R.id.imgloading);
        loading.startAnimation(loadingAnim);
//        scanCard = view.findViewById(R.id.scanCard);

        title = view.findViewById(R.id.tv1);
        classNum = view.findViewById(R.id.classNum);
        attendNum = view.findViewById(R.id.attendNum);
        Rvattentd = view.findViewById(R.id.classNumRe);

    }

    @Override
    protected void setListener() {
        super.setListener();
        for(int i = 0 ;i < linearLayouts.length ;i++){
            linearLayouts[i].setOnClickListener(this);
            textViews[i].setOnClickListener(this);
        }
        last.setOnClickListener(lastListener);
        next.setOnClickListener(nextListener);
    }


    private View.OnClickListener lastListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (CurrentPage <= 1){
                ToastUtil.showToast(context,"当前是第一页");
                return;
            }
            isRunning = false;
            relativeLayout.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            CurrentPage--;
            runThreadAction();
        }
    };

    private View.OnClickListener nextListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (CurrentPage >= pageCount){
                ToastUtil.showToast(context,"当前是最后一页");
                return;
            }
            isRunning = false;
            relativeLayout.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            CurrentPage++;
            runThreadAction();
        }
    };
    @Override
    protected void loadParams() {
        super.loadParams();
    }

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
                selectLinear(0);
                break;
            case R.id.tv1:
                selectLinear(0);
                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(Env.getAppContext().isInitial) {
                constraintLayout1.setVisibility(View.VISIBLE);
                constraintLayout2.setVisibility(View.VISIBLE);
                relativeLayout.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
                runThreadAction();
        }

    }

    @Override
    public void runThreadAction() {
        super.runThreadAction();
    }

    @Override
    protected Object runThreadActionExceute() {
         super.runThreadActionExceute();
        if(Env.getAppContext().StudentId != null){
            return Env.getAppPresenter().getRecordBySid(CurrentPage,28);
        }else{
            return Env.getAppPresenter().getRecord();

        }
    }

    @Override
    protected void runThreadActionSuccess(Object result) {
        super.runThreadActionSuccess(result);
        relativeLayout.setVisibility(View.GONE);
        Rvattentd.setVisibility(View.GONE);
        if(Env.getAppContext().StudentId != null) {
            title.setText("考勤记录");
            getRecordByIdResponse response = (getRecordByIdResponse) result;
            if (response.isSuccess()) {
                list = response.list;
                pageCount = Integer.parseInt(response.PageCount);
                classRecyclerAdapter = new A_recordByIdRecyclerAdapter(context, list);
                recyclerView.setAdapter(classRecyclerAdapter);

            } else {
                ToastUtil.showToast(context, response.getErrMsg());
                isRunning = true;
                return;
            }
        }else{
            title.setText("班级考勤情况");
            getRecordResponse response = (getRecordResponse) result;
            if (response.isSuccess()) {
                list1 = response.list;
                int i = 0;
                for(classAttendRecord classAttendRecord : response.list){
                    if(classAttendRecord.getCardtimes() > 0) i++;
                }
                Rvattentd.setVisibility(View.VISIBLE);
                classNum.setText(list1.size()+"人");
                attendNum.setText(i+"");
                classRecyclerAdapter1 = new A_recordRecyclerAdapter(context, list1);
                recyclerView.setAdapter(classRecyclerAdapter1);

            } else {
                ToastUtil.showToast(context, response.getErrMsg());
                isRunning = true;
                return;
            }

        }
        recyclerView.setVisibility(View.VISIBLE);
        isRunning = true;
    }

    @Override
    protected void runThreadActionError(Exception ex) {
        super.runThreadActionError(ex);
    }

    private void selectLinear(int i){
        if(isRunning){
            isRunning = false;
            SelectedBtnUtil.selectedforLinear(linearLayouts,linearLayouts[i]);
            SelectedBtnUtil.selectedforText(textViews,textViews[i]);
            linearLayouts[i].startAnimation(animation);
            textViews[i].startAnimation(animation);
            name.setText(textViews[i].getText());
            relativeLayout.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            pageCount = 0;
            CurrentPage = 1;
            runThreadAction();
        }

    }


}
